package com.printeverywhere.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.User;
import com.printeverywhere.dto.UserFavoritesUsers;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

public class AddToFavoriteAction extends FrontBaseSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private UserFavoritesUsers userFavoritesUsers;

	private User user;

	private String note;

	private SearchCriteria searchCriteria = new SearchCriteria();

	private String action;

	public AddToFavoriteAction() {
		super(true, false);
	}

	public String addToFavoriteUsers(){
		try{
			if(request.getParameter("action")!=  null){
				action = request.getParameter("action");
			}
			if(request.getParameter(AdminConstants.USERID)!=  null){
				String userId = request.getParameter(AdminConstants.USERID);
				user = UserServiceDelegator.getUser(Integer.parseInt(userId));
			}
		}catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

	public String addAsFavoriteUser(){
		try {
			user = UserServiceDelegator.getUser(user.getId());
			User loginUser = SessionUtil.getUser(request);
			searchCriteria.setAttribute(AdminConstants.USER, loginUser);
			searchCriteria.setAttribute("userIdFav", user);
			List<UserFavoritesUsers> favoriteUser = (List<UserFavoritesUsers>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserFavoritesUsers.class);
			if(favoriteUser.size() > 0){
				addActionMessage("User already added as Favorite.");
				return INPUT;
			}else{
				userFavoritesUsers = new UserFavoritesUsers();
				userFavoritesUsers.setDate(DateUtil.getTodaysDate());
				userFavoritesUsers.setNote(note);
				userFavoritesUsers.setSort(AdminConstants.ZERO_NUMBER);
				userFavoritesUsers.setUser(loginUser);
				userFavoritesUsers.setUserIdFav(user);
				CommonFunctionDelegator.generalInsertbyobj(userFavoritesUsers);
			}
		} catch (Exception e) {
			addActionError("Note is not added.");
			return INPUT;
		}
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public UserFavoritesUsers getUserFavoritesUsers() {
		return userFavoritesUsers;
	}

	public void setUserFavoritesUsers(UserFavoritesUsers userFavoritesUsers) {
		this.userFavoritesUsers = userFavoritesUsers;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}



}
